//*************************************************************************
// File Name: Member.cpp
// This is the implementation file for the member functions
// of Classes Member and SuperMember
// "Group 3"
// CS 1C
// March 25, 2010
// Group Project: SuperWarehouse
// Due: March 25, 2010
// - KZ's comment
//*************************************************************************

#include "Member.h"
#include "functions.h"
#include "config.h"
#include <iostream>
#include <time.h>

//  protected class members
//	memberName
//	memberNumber
//	expirationDate
//	memberType
//	annualDues
//	totalSpent
//	totalRebate

using namespace std;

Member::Member() :
		memberName(""),
		memberNumber(0),
		expirationDate(0),
		memberType(MEMBER_CODE_BASIC),
		annualDues(MEMBER_DUES_BASIC),
		totalSpent(0.00) {
}

Member::Member(string name, int num, time_t exp, char type) :
				memberName(name),
				memberNumber(num),
				expirationDate(exp),
				memberType(type),
				annualDues(MEMBER_DUES_BASIC),
				totalSpent(0.00) {

	if (expirationDate == 0) {
		expirationDate = time(NULL) + 86400 * 365;
	}
}

Member::~Member() {
}

//MEMBER SETTERS
void Member::setMemberData(string name, int num, char type, time_t exp) {
	memberName = name;
	memberNumber = num;
	memberType = type;
	expirationDate = exp;
}

void Member::setName(string name) {
	memberName = name;
}

void Member::setMemberNumber(int memberID) {
	memberNumber = memberID;
}
void Member::setMemberType(char memberLevel) {
	memberType = memberLevel;
}
void Member::setDues(float dues) {
	annualDues = dues;
}
void Member::setExpirationDate(time_t exp) {
	expirationDate = exp;
}
void Member::setTotalSpent(float spent) {
	totalSpent = spent;
}
void Member::addPurchase(float purchase) {
	totalSpent += purchase;
}

//MEMBER GETTERS
string Member::getName() {
	return memberName;
}
int Member::getMemberNumber() {
	return memberNumber;
}
char Member::getMemberType() {
	return memberType;
}
float Member::getDues() {
	return annualDues;
}
time_t Member::getExpirationDate() {
	return expirationDate;
}
float Member::getTotalSpent() {
	return totalSpent;
}

string Member::toString() {
	string ret;
	char buf[1000];

	ret = string();

	sprintf(buf, "Member %d", this->getMemberNumber());
	ret.append(buf).append("\n")
			.append("  Name: ").append(getName()).append("\n")
			.append("  Type: Basic\n")
			.append("  Expiration: ")
			.append(dateStringFromTimestamp(this->expirationDate))
			.append("\n");

	sprintf(buf, "$%0.2f", getTotalSpent());
	ret.append("  Total Spent: ").append(buf).append("\n");

	sprintf(buf, "$%0.2f", getDues());
	ret.append("  Dues: ").append(buf).append("\n");

	return ret;
}

bool Member::operator==(const Member &other) const {
	if (this->memberNumber == other.memberNumber) {
		return true;
	} else {
		return false;
	}
}

Member &Member::operator=(const Member &rhs) {
	if (this != &rhs) {
		this->annualDues = rhs.annualDues;
		this->expirationDate = rhs.expirationDate;
		this->memberName = rhs.memberName;
		this->memberNumber = rhs.memberNumber;
		this->memberType = rhs.memberType;
		this->totalSpent = rhs.totalSpent;
	}
	return *this;
}


SuperMember::SuperMember() : Member(), totalRebate(0) {
	annualDues = MEMBER_DUES_SUPER;
}

SuperMember::SuperMember(string name, int ID, time_t exp, char type,
		float rebate) :
		Member(name, ID, exp, type),
		totalRebate(rebate) {
	annualDues = MEMBER_DUES_SUPER;
}

SuperMember::~SuperMember() {
}

void SuperMember::addRebate(float rebate) {
	totalRebate = totalSpent * .03;
}

//SUPER MEMBER GETTERS
float SuperMember::getRebate() {
	return totalRebate;
}

void SuperMember::addPurchase(float purchase) {
	Member::addPurchase(purchase);
	totalRebate += purchase * (SUPER_MEMBER_REBATE_PERCENT / 100);
}

string SuperMember::toString() {
	string ret;
	char buf[1000];

	ret = string();

	sprintf(buf, "Member %d", this->getMemberNumber());
	ret.append(buf).append("\n")
			.append("  Name: ").append(getName()).append("\n")
			.append("  Type: Super\n")
			.append("  Expiration: ")
			.append(dateStringFromTimestamp(this->expirationDate))
			.append("\n");

	sprintf(buf, "$%0.2f", getTotalSpent());
	ret.append("  Total Spent: ").append(buf).append("\n");

	sprintf(buf, "$%0.2f", getDues());
	ret.append("  Dues: ").append(buf).append("\n");

	sprintf(buf, "$%0.2f", getRebate());
	ret.append("  Rebate: ").append(buf).append("\n");

	return ret;
}

bool SuperMember::operator==(const SuperMember &other) const {
	if (this->memberNumber == other.memberNumber) {
		return true;
	} else {
		return false;
	}
}

SuperMember &SuperMember::operator=(const SuperMember &rhs) {
	if (this != &rhs) {
		this->annualDues = rhs.annualDues;
		this->expirationDate = rhs.expirationDate;
		this->memberName = rhs.memberName;
		this->memberNumber = rhs.memberNumber;
		this->memberType = rhs.memberType;
		this->totalSpent = rhs.totalSpent;
		this->totalRebate = rhs.totalRebate;
	}
	return *this;
}

